package ylyhappy.cn.ideavim.lg.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class MyDatabaseHelper(val context: Context, name: String, version: Int)
    : SQLiteOpenHelper(context, name, null, version) {

    private val createAction =
        "create table Actions ( id integer primary key autoincrement, en text, zh text, tag text)"
    private val createConfig =
        "create table Config (id integer primary key autoincrement, line_id integer, comment text, name text)"
    private val createConfigAndLine =
        "create table ConfigAndLine (id integer primary key autoincrement, config_id integer, line_id integer)"
    private val createLine =
        "create table Line (id integer primary key autoincrement, action_id integer, map text, actionpre text, shortcut text, actionafter text, cr text, source text)"

    /**
     * @param db The database.
     */
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(createAction)
        db.execSQL(createConfig)
        db.execSQL(createConfigAndLine)
        db.execSQL(createLine)
        initActions(db);
        initLine(db);
        createDefaultConfig(db);
    }

    /**
     * @param db The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        var cout = oldVersion;

        if (cout < newVersion && cout == 1) {
            cout++;
        }

        if (cout < newVersion && cout == 2) {
            cout++;
        }

        if (cout < newVersion && cout == 3) {
            cout++;
        }

        if (cout < newVersion && cout == 4) {
            cout++;
        }
    }


}